<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Margin与Padding的区别</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f7fa;
            padding: 20px;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
        }

        header {
            text-align: center;
            margin-bottom: 40px;
            padding: 20px;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }

        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
        }

        .content {
            display: flex;
            flex-wrap: wrap;
            gap: 30px;
            margin-bottom: 40px;
        }

        .explanation {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }

        .demo {
            flex: 1;
            min-width: 300px;
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
        }

        h2 {
            color: #2575fc;
            margin-bottom: 15px;
            padding-bottom: 10px;
            border-bottom: 2px solid #f0f0f0;
        }

        .definition {
            margin-bottom: 20px;
            padding: 15px;
            border-left: 4px solid #6a11cb;
            background-color: #f9f9f9;
        }

        .comparison {
            display: grid;
            grid-template-columns: 1fr 1fr;
            gap: 15px;
            margin-top: 20px;
        }

        .margin-box, .padding-box {
            padding: 15px;
            border-radius: 8px;
            text-align: center;
        }

        .margin-box {
            background-color: #e3f2fd;
            border: 2px dashed #2196f3;
        }

        .padding-box {
            background-color: #e8f5e9;
            border: 2px dashed #4caf50;
        }

        .visual-demo {
            position: relative;
            height: 300px;
            background-color: #f0f0f0;
            border: 1px solid #ddd;
            border-radius: 8px;
            margin-top: 20px;
            overflow: hidden;
        }

        .margin-area {
            position: absolute;
            top: 40px;
            left: 40px;
            right: 40px;
            bottom: 40px;
            background-color: #bbdefb;
            border: 2px dashed #64b5f6;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .border-area {
            position: absolute;
            top: 70px;
            left: 70px;
            right: 70px;
            bottom: 70px;
            background-color: #ffebee;
            border: 2px solid #f44336;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .padding-area {
            position: absolute;
            top: 100px;
            left: 100px;
            right: 100px;
            bottom: 100px;
            background-color: #e8f5e9;
            border: 2px dashed #81c784;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .content-area {
            position: absolute;
            top: 130px;
            left: 130px;
            right: 130px;
            bottom: 130px;
            background-color: #fff3e0;
            border: 2px solid #ff9800;
            display: flex;
            align-items: center;
            justify-content: center;
            font-weight: bold;
            color: #e65100;
        }

        .controls {
            margin-top: 20px;
            display: flex;
            flex-wrap: wrap;
            gap: 15px;
        }

        .control-group {
            flex: 1;
            min-width: 200px;
        }

        label {
            display: block;
            margin-bottom: 5px;
            font-weight: 500;
        }

        input[type="range"] {
            width: 100%;
        }

        .value-display {
            text-align: center;
            margin-top: 5px;
            font-size: 0.9rem;
            color: #666;
        }

        .examples {
            background: white;
            padding: 25px;
            border-radius: 10px;
            box-shadow: 0 4px 6px rgba(0, 0, 0, 0.05);
            margin-bottom: 40px;
        }

        .code-example {
            background-color: #f5f5f5;
            padding: 15px;
            border-radius: 5px;
            margin: 15px 0;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
        }

        .example-box {
            padding: 20px;
            margin: 20px 0;
            background-color: #e3f2fd;
            border: 2px solid #2196f3;
        }

        .example-box.padding-example {
            padding: 40px;
            background-color: #e8f5e9;
            border-color: #4caf50;
        }

        .example-box.margin-example {
            margin: 40px 0;
            background-color: #fff3e0;
            border-color: #ff9800;
        }

        footer {
            text-align: center;
            padding: 20px;
            margin-top: 40px;
            color: #666;
            border-top: 1px solid #eee;
        }

        @media (max-width: 768px) {
            .content {
                flex-direction: column;
            }

            .comparison {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1>Margin与Padding的区别</h1>
        <p class="subtitle">理解CSS盒模型中的两个关键概念</p>
    </header>

    <div class="content">
        <div class="explanation">
            <h2>基本概念</h2>

            <div class="definition">
                <h3>Padding（内边距）</h3>
                <p>元素内容与边框之间的空间。增加padding会使元素内容远离边框，但不会影响元素与其他元素的距离。</p>
            </div>

            <div class="definition">
                <h3>Margin（外边距）</h3>
                <p>元素边框与其他元素之间的空间。增加margin会使整个元素（包括边框和padding）远离其他元素。</p>
            </div>

            <div class="comparison">
                <div class="margin-box">
                    <h4>Margin特点</h4>
                    <ul style="text-align: left; margin-top: 10px;">
                        <li>在元素外部</li>
                        <li>控制元素间距离</li>
                        <li>可以是负值</li>
                        <li>不包含背景色</li>
                    </ul>
                </div>

                <div class="padding-box">
                    <h4>Padding特点</h4>
                    <ul style="text-align: left; margin-top: 10px;">
                        <li>在元素内部</li>
                        <li>控制内容与边框距离</li>
                        <li>不能是负值</li>
                        <li>包含背景色</li>
                    </ul>
                </div>
            </div>
        </div>

        <div class="demo">
            <h2>可视化演示</h2>
            <p>调整下面的滑块，观察margin和padding的变化：</p>

            <div class="visual-demo">
                <div class="margin-area">
                    <span>Margin</span>
                </div>
                <div class="border-area">
                    <span>Border</span>
                </div>
                <div class="padding-area">
                    <span>Padding</span>
                </div>
                <div class="content-area">
                    <span>内容区域</span>
                </div>
            </div>

            <div class="controls">
                <div class="control-group">
                    <label for="margin-slider">Margin: <span id="margin-value">20px</span></label>
                    <input type="range" id="margin-slider" min="0" max="80" value="20">
                </div>

                <div class="control-group">
                    <label for="padding-slider">Padding: <span id="padding-value">30px</span></label>
                    <input type="range" id="padding-slider" min="0" max="80" value="30">
                </div>
            </div>
        </div>
    </div>

    <div class="examples">
        <h2>实际应用示例</h2>

        <h3>Padding示例</h3>
        <p>增加padding使按钮内容更易读：</p>
        <div class="code-example">
            .button {<br>
            &nbsp;&nbsp;padding: 10px 20px;<br>
            &nbsp;&nbsp;background-color: #4CAF50;<br>
            &nbsp;&nbsp;color: white;<br>
            &nbsp;&nbsp;border: none;<br>
            &nbsp;&nbsp;border-radius: 4px;<br>
            }
        </div>

        <button style="padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 4px; margin-right: 10px;">
            小padding
        </button>
        <button style="padding: 15px 30px; background-color: #4CAF50; color: white; border: none; border-radius: 4px;">
            大padding
        </button>

        <h3>Margin示例</h3>
        <p>使用margin控制段落间距：</p>
        <div class="code-example">
            p {<br>
            &nbsp;&nbsp;margin-bottom: 20px;<br>
            }
        </div>

        <div style="margin-bottom: 10px; padding: 10px; background-color: #e3f2fd;">
            段落1 (margin-bottom: 10px)
        </div>
        <div style="margin-bottom: 30px; padding: 10px; background-color: #e3f2fd;">
            段落2 (margin-bottom: 30px)
        </div>
        <div style="padding: 10px; background-color: #e3f2fd;">
            段落3
        </div>
    </div>

    <footer>
        <p>© 2023 CSS盒模型教程 | 理解margin和padding的区别</p>
    </footer>
</div>

<script>
    const marginSlider = document.getElementById('margin-slider');
    const paddingSlider = document.getElementById('padding-slider');
    const marginValue = document.getElementById('margin-value');
    const paddingValue = document.getElementById('padding-value');
    const marginArea = document.querySelector('.margin-area');
    const paddingArea = document.querySelector('.padding-area');
    const contentArea = document.querySelector('.content-area');

    function updateMargin() {
        const value = marginSlider.value;
        marginValue.textContent = `${value}px`;

        marginArea.style.top = `${value}px`;
        marginArea.style.left = `${value}px`;
        marginArea.style.right = `${value}px`;
        marginArea.style.bottom = `${value}px`;
    }

    function updatePadding() {
        const value = paddingSlider.value;
        paddingValue.textContent = `${value}px`;

        const margin = parseInt(marginSlider.value);
        paddingArea.style.top = `${margin + 30}px`;
        paddingArea.style.left = `${margin + 30}px`;
        paddingArea.style.right = `${margin + 30}px`;
        paddingArea.style.bottom = `${margin + 30}px`;

        contentArea.style.top = `${margin + 60}px`;
        contentArea.style.left = `${margin + 60}px`;
        contentArea.style.right = `${margin + 60}px`;
        contentArea.style.bottom = `${margin + 60}px`;
    }

    marginSlider.addEventListener('input', updateMargin);
    paddingSlider.addEventListener('input', updatePadding);

    // 初始化
    updateMargin();
    updatePadding();
</script>
</body>
</html>